package com.ruoyi.web.controller.net;

import com.ruoyi.net.mapper.NetGroupMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.net.domain.NetElement;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.NetGroup;
import com.ruoyi.net.service.INetGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元逻辑域Controller
 * 
 * @author ruoyi
 * @date 2024-10-27
 */
@Api(tags = "网元逻辑域")
@RestController
@RequestMapping("/net/group")
public class NetGroupController extends BaseController
{
    @Autowired
    private INetGroupService netGroupService;

    @Autowired
    private NetGroupMapper netGroupMapper;

    /**
     * 查询网元逻辑域列表
     */
    @ApiOperation(value = "查询网元逻辑域列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(NetGroup netGroup)
    {
        startPage();
        List<NetGroup> list = netGroupService.selectNetGroupList(netGroup);
        return getDataTable(list);
    }

    /**
     * 导出网元逻辑域列表
     */
    @ApiOperation(value = "导出网元逻辑域列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:group:export')")
    @Log(title = "网元逻辑域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NetGroup netGroup)
    {
        List<NetGroup> list = netGroupService.selectNetGroupList(netGroup);
        ExcelUtil<NetGroup> util = new ExcelUtil<NetGroup>(NetGroup.class);
        util.exportExcel(response, list, "网元逻辑域数据");
    }

    /**
     * 获取网元逻辑域详细信息
     */
    @ApiOperation(value = "获取网元逻辑域详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(netGroupService.selectNetGroupById(id));
    }

    /**
     * 新增网元逻辑域
     */
    @ApiOperation(value = "新增网元逻辑域", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:group:add')")
    @Log(title = "网元逻辑域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NetGroup netGroup)
    {
        int nameCount = netGroupMapper.checkGroupName(netGroup.getGroupName());
        if (nameCount > 0) {
            return error("新增网元逻辑域'" + netGroup.getGroupName() + "'失败，网元逻辑域已存在");
        }
        return toAjax(netGroupService.insertNetGroup(netGroup));
    }

    /**
     * 修改网元逻辑域
     */
    @ApiOperation(value = "修改网元逻辑域", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('net:group:edit')")
    @Log(title = "网元逻辑域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NetGroup netGroup)
    {
        NetGroup oriNetGroup =  netGroupService.selectNetGroupById(netGroup.getId());
        // 判断修改的名称是否和已有的重复
        if (!oriNetGroup.getGroupName().equalsIgnoreCase(netGroup.getGroupName())) {
            int nameCount = netGroupMapper.checkGroupName(netGroup.getGroupName());
            if (nameCount > 0) {
                return error("修改网元逻辑域'" + netGroup.getGroupName() + "'失败，网元逻辑域已存在");
            }
        }
        return toAjax(netGroupService.updateNetGroup(netGroup));
    }

    /**
     * 删除网元逻辑域
     */
    @ApiOperation(value = "删除网元逻辑域", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('net:group:remove')")
    @Log(title = "网元逻辑域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        // todo 判断是否有子节点
        for (String id : ids) {
            NetGroup netGroup = new NetGroup();
            netGroup.setParentId(id);
            List<NetGroup> list = netGroupService.selectNetGroupList(netGroup);
            if (!list.isEmpty()) {
                return error("删除网元逻辑域失败，该网元逻辑域下存在子节点");
            }
        }
        return toAjax(netGroupService.deleteNetGroupByIds(ids));
    }

    /**
     * 获取拓扑连线
     */
    @ApiOperation(value = "获取拓扑连线", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:group:list')")
    @GetMapping("/topo/{id}")
    public AjaxResult topo(@PathVariable("id") String id) {
        return success(netGroupService.selectTopo(id));
    }

    /**
     * 获取逻辑域树列表
     */
    @ApiOperation(value = "获取逻辑域树列表，groupType:1 逻辑域，2 网元，", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:group:list')")
    @GetMapping("/groupTree")
    public AjaxResult groupTree(NetGroup netGroup) {
        return success(netGroupService.selectGroupTreeList(netGroup));
    }

    /**
     * 批量更新
     */
    @ApiOperation(value = "批量更新逻辑域坐标信息", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('net:element:edit')")
    @Log(title = "网元逻辑域", businessType = BusinessType.UPDATE)
    @PutMapping("/batchUpdate")
    public AjaxResult batchUpdate(@RequestBody List<NetGroup> netGroups) {
        return toAjax(netGroupService.batchUpdate(netGroups));
    }
}